package com.hugo.eduservice.controller;


import com.hugo.commonutiles.R;
import com.hugo.eduservice.client.VodClient;
import com.hugo.eduservice.entity.EduVideo;
import com.hugo.eduservice.service.EduVideoService;
import com.hugo.serviceconfig.exceptionhandler.HugoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author hugo
 * @since 2020-09-08
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    //添加小结
    @PostMapping("addVideo")
    public R addVideo(@RequestBody(required=false) EduVideo eduVideo ){
        videoService.save(eduVideo);
        return R.ok();
    }
    //删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小结Id获取视屏id
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();

        // 判断小节里是否有视屏id
        //根据视频id  远程调用  删除视屏id
        if (!StringUtils.isEmpty(videoSourceId)){
            vodClient.deletedVideo(videoSourceId);

        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();

    }


}

